package personnel.department.pages;

import static personnel.department.pages.constants.PageGroup.MEDIC_PERSONAL;
import static personnel.department.pages.constants.PageGroup.QUERIES;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;

import personnel.department.dao.entity.UniqueObject;
import personnel.department.pages.addpage.AddPersonalPage;
import personnel.department.pages.constants.PageGroup;
import personnel.department.pages.finder.FinderPage;

import com.google.common.base.Preconditions;

public class PagesLogic {
	
	private static PagesLogic instance;
	private boolean isTabFolderHidden;
	private Tree tree;
	private CTabFolder tabFolder;
	private Shell mainShell;
	private PDTable<UniqueObject> searchTable;

	private EmployeeInfoComposite infoComposite;

	private QueryResultComposite resultComposite;
	
	public static PagesLogic getInstance() {
		if (instance == null) {
			instance = new PagesLogic();
		}
		return instance;
	}
	
	public Tree getTree() {
		return tree;
	}

	public void setTree(Tree tree) {
		this.tree = tree;
	}

	public CTabFolder getTabFolder() {
		if (tabFolder == null) {
			createTabFolder();
		}
		if (isTabFolderHidden) {
			setTabFolderHide(false);
		}
		return tabFolder;
	}

	private void createTabFolder() {
		tabFolder = new CTabFolder(getMainShell(), SWT.TOP | SWT.FLAT);
		createTabFolderGridData();
		tabFolder.setLayout(new GridLayout());
		tabFolder.setSimple(false);
		tabFolder.setBackground(getMainShell().getBackground());
		tabFolder.pack();
	}

	private void createTabFolderGridData() {
		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		tabFolder.setLayoutData(gridData);
	}

	public void setTabFolder(CTabFolder tabFolder) {
		this.tabFolder = tabFolder;
	}
	
	public void createPage(PageGroup pageGroup) {
		Preconditions.checkNotNull(pageGroup, "Page group can't be null");
		switch (pageGroup) {
		case MEDIC_PERSONAL:
			new SearchPersonalPage(MEDIC_PERSONAL.getGroup()).create();
			break;
		case ADD_PERSON:
			new AddPersonalPage().createContent();
			break;
		case QUERIES:
			new FinderPage(QUERIES.getGroup()).create();
			break;
		default:
//			new SettingsPage("Settings").create();
		}
	}

	public Shell getMainShell() {
		return mainShell;
	}

	public void setMainShell(Shell mainShell) {
		this.mainShell = mainShell;
	}
	
	public void setSearchTable (PDTable<UniqueObject> table) {
		this.searchTable = table;
	}
	
	public PDTable<UniqueObject> getSearchTable() {
		return searchTable;
	}
	
	public EmployeeInfoComposite getInfoComposite() {
		return infoComposite;
	}

	public void setInfoComposite(EmployeeInfoComposite infoComposite) {
		this.infoComposite = infoComposite;
	}

	public void setResultComposite(QueryResultComposite resultComposite) {
		this.resultComposite = resultComposite;
	}
	
	public QueryResultComposite getResultComposite() {
		return resultComposite;
	}
	
	public CTabItem createTabItem(String tabName) {
		CTabItem tabItem = new CTabItem(tabFolder, SWT.CLOSE);
		tabItem.setText(tabName);
		tabItem.addDisposeListener(e -> {
			if (tabFolder.getItemCount() < 1) {
				setTabFolderHide(true);
			}
		});
		return tabItem;
	}

	private void setTabFolderHide(boolean value) {
		((GridData)tabFolder.getLayoutData()).exclude = value;
		tabFolder.setVisible(!value);
		isTabFolderHidden = value;
	}
	

}
